package com.uchedao.vehicledetection.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonObject;
import com.uchedao.mybaselibrary.http.VolleyUtil;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.activity.MainActivity;
import com.uchedao.vehicledetection.config.Api;
import com.uchedao.vehicledetection.global.GlobalInfo;
import com.uchedao.vehicledetection.model.MyCarResouceEntity;
import com.uchedao.vehicledetection.network.HttpRequest;
import com.uchedao.vehicledetection.utils.ProgressDialogUtil;
import com.uchedao.vehicledetection.utils.T;
import com.uchedao.vehicledetection.view.widget.dialog.ConfirmCheckCarDialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hang on 2015/9/14.
 */
public class MyCarSourceAdapter extends CommonAdapter<MyCarResouceEntity> {

    /**
     * 1 待验车辆
     * default 我的车源
     */
    public int style;

    private ConfirmCheckCarDialog dlg;

    public MyCarSourceAdapter(Context context, List<MyCarResouceEntity> data) {
        super(context, data, R.layout.item_my_car_source);
        dlg = new ConfirmCheckCarDialog(context);
    }

    @Override
    public void conver(ViewHolder holder, final MyCarResouceEntity item, int position) {
        holder.setText(R.id.tvMyCarItemTitle, item.getTitle());
        holder.setText(R.id.tvMyCarItemName, item.getReal_name());
        holder.setText(R.id.tvMyCarItemMobile, item.getMobile());
        holder.setText(R.id.tvMyCarItemNumber, item.getCar_number());
        holder.setText(R.id.tvMyCarItemAddr, item.getCheck_position());
        holder.setText(R.id.tvMyCarItemDate, item.getCreated_at());
        holder.setText(R.id.tvMyCarItemStatus, item.getStatus_cn());

        Button btn = holder.getView(R.id.btnMyCarItemCheck);
        switch(style) {
            case 1:
                btn.setText(R.string.btn_want_validate_car);
                break;

            default:
                btn.setText(R.string.btn_begin_validate_car);
                break;
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch(style) {
                    case 1:
                        // 待验车源
                        dlg.setCarId(item.getId());
                        dlg.setRequestCallback(new ConfirmCheckCarDialog.RequestCallback() {
                            @Override
                            public void requestCompleted() {
                                getmData().remove(item);
                                notifyDataSetChanged();
                            }
                        });
                        dlg.show();
                        break;

                    default:
                        // 我的车源
                        requestCheck(item);
                        break;
                }
            }
        });
    }

    private void requestCheck(final MyCarResouceEntity entity) {
        ProgressDialogUtil.showProgressDlg(mContext, "开始验车");
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", entity.getId());
        Request req = HttpRequest.getRequest(HttpRequest.POST, Api.Action.BEGIN_CHECK_CAR, params,
                new Response.Listener<JsonObject>() {
                    @Override
                    public void onResponse(JsonObject response) {
                        GlobalInfo.dbHelper.insertDraf(entity.getCar_number(), entity.getVin(), entity.getId());
                        GlobalInfo.draft =GlobalInfo.dbHelper.getDraft(entity.getCar_number());

                        Intent it = new Intent(mContext, MainActivity.class);
                        mContext.startActivity(it);
                        ProgressDialogUtil.dismissProgressDlg();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        ProgressDialogUtil.dismissProgressDlg();
                        T.showShort(mContext, error.getMessage());
                    }
                });
        VolleyUtil.getInstance(mContext).getRequestQueue(mContext).add(req);
    }
}
